package com.ghxxzx.daglcustom.business_form.web;

import com.ghxxzx.daglcustom.business_form.domain.FormValsPo;
import com.ghxxzx.daglcustom.business_form.service.FormValsService;
import com.alibaba.fastjson.JSONObject;
import org.activiti.engine.TaskService;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.tmsps.ne4spring.base.BaseAction;
import org.tmsps.ne4spring.utils.wrapper.WrapMapper;
import org.tmsps.ne4spring.utils.wrapper.Wrapper;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/form/vals")
public class FormValsAction extends BaseAction {

	@Resource
	private TaskService taskService;
	@Autowired
	private FormValsService formValsService;

	// 获取已填写的表单数量
	@GetMapping("/get_forms")
	public Wrapper<List<FormValsPo>> get_forms(String task_id) {
		Task task = taskService.createTaskQuery().taskId(task_id).singleResult();
		if (task == null) {
			return WrapMapper.ok();
		}

		logger.info(task.toString());
		FormValsPo formValsPo = formValsService.getFormValByTaskId(task_id);

		List<FormValsPo> allBeforFormsList = new ArrayList<>();
		if (formValsPo != null) {
			allBeforFormsList = formValsService.getAllBeforForms(task.getProcessInstanceId(), formValsPo.getSortz());
		} else {
			allBeforFormsList = formValsService.getAllBeforForms(task.getProcessInstanceId(), task_id);
		}

		return WrapMapper.ok(allBeforFormsList);
	}

	// 获取已填写的表单
	@GetMapping("/get_json_formval")
	public Wrapper<JSONObject> get_json_formval(String form_val_kid) {
		FormValsPo formVals = bs.findById(form_val_kid, FormValsPo.class);

		JSONObject result = new JSONObject();
		result.put("formVals", formVals);
		return WrapMapper.ok(result);
	}
}
